<template>
  <div>
    <p class="pinformation" style="margin-top: 7px">搜索条件</p>
    <el-form label-width="100px" :model="queryParams" ref="queryRef">
      <el-row>
        <el-col :span="4">
          <el-form-item label="报告编号：" prop="reportCode">
            <el-input
              v-model="queryParams.reportCode"
              clearable
              maxlength="100"
              @keyup.enter="handeleQuery"
              placeholder="请输入报告编号"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="报告名称：" prop="reportName">
            <el-input
              v-model="queryParams.reportName"
              clearable
              maxlength="100"
              @keyup.enter="handeleQuery"
              placeholder="请输入报告名称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="委托单位：" prop="wtCustomName">
            <el-input
              v-model="queryParams.wtCustomName"
              clearable
              maxlength="100"
              @keyup.enter="handeleQuery"
              placeholder="请输入委托单位"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="受检单位：" prop="sjCustomName">
            <el-input
              v-model="queryParams.sjCustomName"
              clearable
              maxlength="100"
              @keyup.enter="handeleQuery"
              placeholder="请输入受检单位"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="报告类型：" prop="b">
            <el-input
              v-model="queryParams.b"
              clearable
              maxlength="100"
              @keyup.enter="handeleQuery"
              placeholder="请输入报告类型"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="紧急类型：" prop="degree">
            <el-select clearable v-model="queryParams.degree">
              <el-option
                v-for="dict in proxy.addListNoLimit(sys_degree)"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <el-form-item label="方案编号：" prop="projectCode">
            <el-input
              v-model="queryParams.projectCode"
              clearable
              maxlength="100"
              @keyup.enter="handeleQuery"
              placeholder="请输入方案编号"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="方案名称：" prop="projectName">
            <el-input
              v-model="queryParams.projectName"
              clearable
              maxlength="100"
              @keyup.enter="handeleQuery"
              placeholder="请输入方案名称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="报告模板：" prop="templateId">
            <el-select
              v-model="queryParams.templateId"
              placeholder="请选择报告模板"
              clearable
              filterable
              style="width: 100%"
            >
              <el-option
                v-for="item in templateList"
                :key="item.templateId"
                :label="item.templateName"
                :value="item.templateId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <!-- ！ -->
        <el-col :span="4">
          <!-- planDate -->
          <el-form-item label="编制时间：">
            <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              :clearable="false"
              :editable="false"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="合同编号：" prop="contractCode">
            <el-input
              v-model="queryParams.contractCode"
              clearable
              maxlength="100"
              @keyup.enter="handeleQuery"
              placeholder="请输入合同编号"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-row justify="end">
            <el-button type="success" @click="handeleQuery">搜索</el-button>
            <el-button class="business resetBtn" @click="resetQuery">重置</el-button>
          </el-row>
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <p class="pinformation">{{ props.archiveStatus == 1 ? "未" : "已" }}归档报告列表</p>
      <p class="totalStyle" @dblclick="test()">共 {{ total }} 条数据</p>
    </el-row>
    <el-table ref="mainTableRef" :data="projectList" v-loading="loading">
      <el-table-column prop="reportCode" label="报告编号" align="center" />
      <el-table-column prop="reportName" label="报告名称" align="center" />
      <el-table-column prop="wtCustomName" label="委托单位" align="center" />
      <el-table-column prop="sjCustomName" label="受检单位" align="center" />
      <el-table-column prop="projectCode" label="方案编号" align="center" />
      <el-table-column prop="projectName" label="方案名称" align="center" />
      <el-table-column prop="updateTime" label="编制时间" align="center" />
      <el-table-column prop="displaySize" label="文件大小" align="center" />
      <el-table-column prop label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="success"
            @click="handleAchvieFile(scope.row)"
            v-if="props.archiveStatus == 1 && !scope.row.archiveFileId && scope.row.reportId"
            >生成存档文件</el-button
          >
          <template v-if="scope.row.archiveFileId">
            <el-button
              link
              type="success"
              v-if="props.archiveStatus == 1"
              @click="review(scope.row)"
              >预览</el-button
            >
            <el-button
              link
              type="success"
              v-if="props.archiveStatus == 1"
              @click="handleDownload(scope.row)"
              >下载</el-button
            >
            <el-button
              link
              type="success"
              v-if="props.archiveStatus == 2"
              @click="mation(scope.row)"
              >详情</el-button
            >
            <el-button
            link
            type="success"
            @click="handleAchvieFile(scope.row)"
            v-if="scope.row.reportId"
            >重新生成存档文件</el-button
          >
          </template>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="handeleQuery"
    />
  </div>
</template>

<script setup>
import { getArchiveList,batchCreate } from "@/api/projectArchive/index";
import { getTemplateList,createAchvieFile } from "@/api/report/reportProject";

const { proxy } = getCurrentInstance();
const { sys_degree } = proxy.useDict("sys_degree");
const router = useRouter();
const projectList = ref([]);
const total = ref(0);
const dateRange = ref([]);
const loading = ref(false);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    degree: -1,
  },
});
const { queryParams, form } = toRefs(data);
const props = defineProps({
  archiveStatus: {
    type: Number, //参数类型
  },
});
const emits = defineEmits(["archiveFileId", "archiveFileId2"]);

function handeleQuery() {
  queryParams.value.archiveStatus = props.archiveStatus;
  loading.value = true;
  getArchiveList(proxy.addDateRange(queryParams.value, dateRange.value)).then((res) => {
    loading.value = false;
    projectList.value = res.rows;
    total.value = res.total;
  });
}
function handleAchvieFile(row) {
  createAchvieFile(row.reportId).then((res) => {
    proxy.$modal.msgSuccess(res.msg);
  });
}

function review(row) {
  emits("archiveFileId", row);
}

function handleDownload(row) {
  proxy.$modal.loading("正在下载文件，请稍候...");
  debugger;
  proxy.$download.ossFile(row.bucketName, row.fileName).then(() => {
    proxy.$modal.closeLoading();
  });
}

function mation(row) {
  return;
  emits("archiveFileId2", row.archiveFileId);
}

function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handeleQuery();
}

defineExpose({
  getTableHeight,
});

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 22 }];
    }
    getTableHeight();
  }
);
function test(){
  if(!queryParams.value.reportCode){
    return;
  }
  let number = parseInt(queryParams.value.reportCode);
  batchCreate(number).then(res => {
    alert("执行成功！");
  })
}
const heightParams = ref([{ tableName: "main", bottomHeight: 22 }]);
function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
});
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});
handeleQuery();

const templateList = ref([]);
getTemplateList().then((res) => {
  templateList.value = res.data;
});
</script>

<style></style>
